<?php

namespace App\Supports\Payment\Methods\Sandwap;

use Exception;
use App\Supports\Payment\Methods\Method;
use App\Supports\Payment\Contracts\MethodPay;
use Illuminate\Validation\ValidationException;

class PayGoldCharge extends Method implements MethodPay
{
	public function validate($order = null)
	{
		if ($order->isPaid()) {
			fail('目标订单已支付');
		}

		if (!$order->user->pay_code) {
			fail('请先设置交易密码');
		}
		if (!$order->user->cert_number) {
			fail('请先绑定银行卡账号');
		}
	}
	
	public function info($order = null)
	{
		$outTradeNo = 'gold_charge_'.$order->number;
		$subject = '支付风速秒订单: ' . $order->number;
		$totalAmount = $order->total_amount;
		
		return [
			'out_trade_no' => $outTradeNo,
			'total_amount' => $totalAmount,
			'subject' => $subject,
			'card_no' => $order->user->cert_number,
			'user_id' => $order->user_id,
			'user_name' => $order->user->real_name,
		];
	}
	
	public function pay($order = null)
	{
		

		$this->validate($order);

		try {
			$info = $this->info($order);
			return $this->channel->pay($info);
		} catch (Exception $exception) {
			return fail('支付暂不可用!');
		}
	}
} 
